package com.fitso.model.util.common;

import static java.math.MathContext.DECIMAL32;

import java.math.BigDecimal;
import java.math.MathContext;

public class MathUtils {
	/**
	 * Simple equality check of 2 {@link BigDecimal}s limiting the evaluation to
	 * 7 digits as prescribed by {@link MathContext.DECIMAL32}
	 * 
	 * @param bd1
	 * @param bd2
	 * @return true/false if the values are equal upto 7 digits
	 */
	public static boolean equals(BigDecimal bd1, BigDecimal bd2) {
		if (bd1 == null && bd2 == null) {
			return true;
		}

		if (bd1 == null && bd2 != null) {
			return false;
		}

		if (bd1 != null && bd2 == null) {
			return false;
		}

		return bd1.round(DECIMAL32).compareTo(bd2.round(DECIMAL32)) == 0;
	}

	/**
	 * Returns the lower of the 2 provided values
	 * 
	 * @param val1
	 * @param val2
	 * @return
	 */
	public static int least(int val1, int val2) {
		return val1 <= val2 ? val1 : val2;
	}
}
